package com.zhan.webflux.controller;

import org.springframework.http.HttpStatusCode;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.stereotype.Controller;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.result.view.Rendering;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/*
 *@Description<p></p>
 *@Author gz
 *@CreateTime 2025/7/22 10:06
 *@CopyRight @2021-2025 卓望信息技术（北京）公司
 */
//@RestController
@Controller
public class TestController {
    @GetMapping("/hello")
    @ResponseBody
    public String getHello(@RequestParam(name = "key", required = false, defaultValue = "haha") String key) {
        return "Hello " + key + "!";
    }

    @GetMapping("/haha")
    public Mono<String> getWorld(@RequestParam(name = "key", required = false, defaultValue = "haha") String key) {
        return Mono.just("Hello " + key + "!");
    }
    

    @GetMapping(value = "/exception")
    public Mono<String> getexception() {
        return Mono.just(0)
                .map(integer -> 10 / integer)
                .map(integer -> "result-" + integer);

    }


    @GetMapping(value = "/exception1")
    public ErrorResponse getexception1() {

        return ErrorResponse.create(new Exception("测四异常"), HttpStatusCode.valueOf(404), "资源未找到");
        //return ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(404), "资源未找到");


    }

    @GetMapping(value = "/H")
    public Rendering getH(ServerWebExchange serverWebExchange) {
        return Rendering.redirectTo("https://www.baidu.com").build();

      /*   DataBuffer wrap = dataBufferFactory.wrap(map.toString().getBytes());
        return response.writeWith(Mono.just(wrap)); */
        /* return Mono.just(0)
                .map(integer -> 10 / integer)
                .map(integer -> "result-" + integer); */

    }


    @PostMapping("/file")
    public String handle(
            @RequestPart("file") FilePart file) {
        return file.filename();
    }
}
